<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <b></b>
        <!-- this is comment -->
        <strong>
            <span>
                <i class="demo"></i>
            </span>
        </strong>
        <i></i>
        <span></span>
        <address></address>
    </div>
    <script>

        function retParent(elem, num) {
            // 返回第num个父级元素
            var count = 0;
            while(count < num) {
                elem = elem.parentElement;
                count ++;
                if(elem === null){
                    count = num;
                }
            }
            return elem;
        }

        var i = document.getElementsByTagName('i')[0];
        console.log(retParent(i, 3));


        Element.prototype.myChildren = function() {
            //自己封装的children方法
            var child = this.childNodes;
            var len = child.length;
            var arr = [];
            for(var i = 0; i < len; i ++) {
                if(child[i].nodeType == 1) {
                    arr.push(child[i]);
                }
            }
            return arr;
        }

        var div = document.getElementsByTagName('div')[0];
        console.log(div.myChildren());

        function retSibling(e, n) {
            //返回e的第n个兄弟元素节点
            while(e && n){
                if(n > 0){
                    if(e.nextElementSibling){
                        e = e.nextElementSibling;
                    }else{
                        e = e.nextSibling;
                        while(e !== null && e.nodeType != 1){
                            e = e.nextSibling;
                        }
                    }
                    
                    n --;
                } else {
                    if(e.previousElementSibling){
                        e = e.previousElementSibling;
                    }else{
                        e = e.previousSibling;
                        while(e !== null && e.nodeType != 1){
                            e = e.previousSibling;
                        }
                    }
                    n ++;
                }
            }
            return e;
        }

        var strong = document.getElementsByTagName('strong')[0];

        console.log(retSibling(strong, 7));

    </script>
</body>
</html>